<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script>
        const canvas = document.querySelector("canvas");
        const ctx = canvas.getContext("2d");
        ctx.fillRect(0,0,canvas.width,canvas.height)
        
        ctx.lineWidth=0.1;
        fadeLineIn()
        function drawLine(x1, y1, x2, y2) {
            ctx.save()
            ctx.beginPath(); //不写每次都会重绘上次的线
            var grd=ctx.createLinearGradient(x1, y1, x2, y2);
                grd.addColorStop(0,"rgba(255,255,255,0)");
                grd.addColorStop(0.3,"rgba(255,255,255,1)");
                grd.addColorStop(0.7,"rgba(255,255,255,1)");
                grd.addColorStop(1,"rgba(255,255,255,0)");
            ctx.strokeStyle=grd
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();
            ctx.closePath()
            ctx.restore()
        }
        function fadeLineIn() {
            if(ctx.lineWidth>=0.5) {
                return;
            }
            ctx.lineWidth+=0.02;
            drawLine(50,100,200,100)
            window.requestAnimationFrame(fadeLineIn)
        }
        
    </script>
</body>
</html>